import time

import allure
from appium.webdriver.common.appiumby import AppiumBy

from tests.app.pages.address_book_page import AddressBookPage
from tests.app.pages.group_chat_page import GroupChatPage
from tests.app.pages.query_page import QueryPage
from tests.app.pages.wecom import Wecom
from tests.app.pages.work_bench_page import WorkBenchPage

@allure.feature("主页面")
class MainPage(Wecom):
    __ADDRESS_BOOK = AppiumBy.XPATH, '//*[@text = "通讯录"]'
    __GROUP_CHAT = AppiumBy.XPATH, "//*[@text='一家公司'and@resource-id = 'com.tencent.wework:id/h8_']"
    __WORK_BENCH = AppiumBy.XPATH, "//*[@text = '工作台']"
    __MAIN_MIL = AppiumBy.ID, "com.tencent.wework:id/mil"
    __SELF_NAME = AppiumBy.ID, "com.tencent.wework:id/lq8"
    __QUERY_BUTTON = AppiumBy.ID, 'com.tencent.wework:id/mi7'

    @allure.step("进入通讯录界面")
    def goto_address_book_page(self):
        self.find_and_click(*self.__ADDRESS_BOOK)
        return AddressBookPage(self.driver)

    @allure.step("进入群聊界面")
    def goto_group_chat_page(self):
        self.find_and_click(*self.__GROUP_CHAT)
        return GroupChatPage(self.driver)

    @allure.step("进入工作台界面")
    def goto_work_bench_page(self):
        self.find_and_click(*self.__WORK_BENCH)
        return WorkBenchPage(self.driver)

    def get_self_name(self):
        with allure.step("获取当前用户名"):
            self.find_and_click(*self.__MAIN_MIL)
            name = self.find(*self.__SELF_NAME)
            self.find_and_click(*self.__MAIN_MIL)
        return name.text

    @allure.step("进入查询界面")
    def goto_query_page(self):
        self.find_and_click(*self.__QUERY_BUTTON)
        return QueryPage(self.driver)